/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.test_finder;

import picounit.Test;
import picounit.TestCase;
import picounit.features.test_factory.TestFactory;
import picounit.util.ClassMarker;
import picounit.util.Equal;
import picounit.verify.constraint.EqualUtil;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import junit.framework.TestSuite;

class SinglePicoUnitTestSuite extends TestSuite implements Equal<SinglePicoUnitTestSuite> {
	private final Class<?> testClass;

	public SinglePicoUnitTestSuite(Class<?> testClass, TestFactory testFactory) {
		this(testClass.getName().replace('$', '.'), testClass, testFactory);
	}

	public SinglePicoUnitTestSuite(String testName, Class<?> testClass, TestFactory testFactory) {
		super(testName);

		validate(testClass);

		this.testClass = testClass;

		Method[] methods = testClass.getMethods();

		for (Method method : methods) {
			if (isPublic(method) &&
				!isStatic(method) &&
				!isIgnored(method) &&
				!isFixtureClass(method.getDeclaringClass()) &&
				!isFrameworkMethod(method)) {

				addTest(testFactory.create(testClass, method));
			}
		}

		if (testCount() == 0) {
			addTest(new NoTestsFoundTestCase(testClass, new ClassMarker(Test.class)));
		}
	}

	public Class getTestClass() {
		return testClass;
	}
	
	public boolean hasName(String name) {
		return getName().equals(name);
	}

	public boolean usesTestClass(Class testClass) {
		return getTestClass().equals(testClass);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Equal methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean equalTo(SinglePicoUnitTestSuite other) {
		return other.hasName(getName()) &&
			other.usesTestClass(getTestClass());
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean equals(Object object) {
		return EqualUtil.isEqual(this, object);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	private void validate(Class testClass) {
		if (testClass.isInterface()) {
			throw new IllegalArgumentException(testClass.getName() + " is an interface");
		}

		if (Modifier.isAbstract(testClass.getModifiers())) {
			throw new IllegalArgumentException(testClass.getName() + " is abstract");
		}
	}

	private boolean isPublic(Method method) {
		return Modifier.isPublic(method.getModifiers());
	}

	private boolean isStatic(Method method) {
		return Modifier.isStatic(method.getModifiers());
	}

	private boolean isFrameworkMethod(Method method) {
		String methodName = method.getName();

		return methodName.equals("mock") ||
			methodName.equals("setUp") ||
			methodName.equals("tearDown") ||
			methodName.equals("ignoredWhen") ||
			methodName.equals("describeNarrative");
	}

	private boolean isFixtureClass(Class declaringClass) {
		return declaringClass.equals(Object.class) ||
			declaringClass.equals(TestCase.class) ||
			declaringClass.equals(junit.framework.TestCase.class);
	}

	private boolean isIgnored(Method method) {
		String methodName = method.getName();

		return methodName.startsWith("ignore_") ||
			methodName.endsWith("_ignore");
	}
}
